import json
import excelUtil

douyinHttp = 'https://aweme-hl.snssdk.com/'

def dySearch(flow):
    httpStart = 'https://aweme-hl.snssdk.com/aweme/v1/discover/search/?'
    if flow.request.url.startswith(httpStart):
        text = json.loads(flow.response.text)
        if text != 'null':
            keyword = text.get('input_keyword')
            users = text.get('user_list')
            resultData = [[keyword]]
            if (users != 'null' and len(users) > 0):
                firstUser = users[0]
                print('==========================' + keyword)
                print(firstUser)
                userName = firstUser.get('user_info').get('nickname')
                shareUrl = 'https://www.iesdouyin.com/share/user/' + firstUser.get('user_info').get('uid')
                if excelUtil.isContains(userName, keyword):
                    resultData[0] = [keyword, userName, shareUrl]
        excelUtil.write_excel_xls_append(excelUtil.FILE_PATH, '抖音', resultData)